package com.schoolMall.utils;

import com.schoolMall.dto.ImageHolder;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author:zyh
 * @Time:2021-08-29-14:19
 * @email:1269231889@qq.com
 */
public class ImgUtil {
    private static Logger logger = LoggerFactory.getLogger(ImgUtil.class);
    private static final String BASE_PATH = "D:\\桌面\\项目\\schoolMall\\src\\main\\resources\\";
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final Random RANDOM = new Random();

    public static String generateThumbnail(ImageHolder imageHolder, String imagePath) throws IOException {
        String randomFileName = getRandomFileName();
        String suffix = getFileSuffix(imageHolder.getImageName());
        makeDirPath(imagePath);
        String relativeAddr = imagePath + randomFileName + suffix;
        File dest = new File(relativeAddr);
        logger.info("dest:{}",dest);
        try {
            Thumbnails.of(imageHolder.getImageInputStream()).size(200, 200)
                    .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(BASE_PATH + "waterMark.jpg")), 0.25f)
                    .outputQuality(0.8f)
                    .toFile(dest);
        } catch (IOException e) {
            throw new IOException("图片修改异常");
        }
        return relativeAddr;
    }

    public static String generateNomralImg(ImageHolder imageHolder, String imagePath) throws IOException {
        String randomFileName = getRandomFileName();
        String fileSuffix = getFileSuffix(imageHolder.getImageName());
        makeDirPath(imagePath);
        String realAddr = imagePath + randomFileName + fileSuffix;
        File dest = new File(realAddr);
        logger.info("dest:{}",dest);
        try{
            Thumbnails.of(imageHolder.getImageInputStream()).size(337, 650)
                    .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(BASE_PATH + "waterMark.jpg")), 0.25f)
                    .outputQuality(0.9f)
                    .toFile(dest);
        } catch (IOException e) {
            throw new IOException("图片存放异常");
        }
        return realAddr;
    }

    /**
     * 如果为文件，删除文件，为目录，删除整个目录
     *
     * @param storePath
     */
    public static void deleteFileOrPath(String storePath) {
        File file = new File(storePath);
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    files[i].delete();
                }
                file.delete();
            }
        }
    }


    /**
     * 获取随机文件名
     *
     * @return
     */
    public static String getRandomFileName() {
        int num = RANDOM.nextInt(899999) + 10000;
        String nowTime = SIMPLE_DATE_FORMAT.format(new Date());
        return nowTime + num;
    }

    /**
     * 创建目录
     *
     * @param target
     */
    private static void makeDirPath(String target) {
        String filePath = target;
        File file = new File(filePath);
        if (!file.isDirectory()) {
            file.mkdirs();
        }
    }

    /**
     * 获取文件类型
     *
     * @param fileName
     * @return
     */
    private static String getFileSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }

}
